package cn.imnaer.service.rpc.userinfo.impl;

import cn.imnaer.service.persistence.model.UserPermission;
import cn.imnaer.service.rest.resquest.model.SettingUserPermissionParams;
import cn.imnaer.service.rest.view.model.UserPermissionResponse;
import cn.imnaer.service.rpc.userinfo.FriendRelationComponentRpcApi;
import cn.imnaer.service.rpc.userinfo.UserPermissionComponentRpcApi;
import cn.imnaer.service.rpc.userinfo.dao.UserPermissionDaoImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/5/22.
 */
public class UserPermissionComponentRpcApiImpl implements UserPermissionComponentRpcApi {

    private UserPermissionDaoImpl userPermissionActiveRecord = UserPermissionDaoImpl.getInstance();
    private FriendRelationComponentRpcApi friendRelationComponentRpcApi = new FriendRelationComponentRpcApiImpl();


    public boolean updateUserPermission(String userId, SettingUserPermissionParams params) throws RuntimeException {
        UserPermission userPermission = this.getUserPermission(userId);
        UserPermission settingPermission = params.caseToUserPermission();
        settingPermission.setUserId(userId);
        settingPermission.setPermissionId(userPermission.getPermissionId());
        this.userPermissionActiveRecord.update(settingPermission);
        return true;
    }

    public UserPermission getUserPermission(String userId) throws RuntimeException{
        UserPermission userPermission = this.userPermissionActiveRecord.findByUserId(userId);
        if(userPermission == null) {
            throw new RuntimeException("用户未初始化权限表, 请联系管理员");
        }
        return userPermission;
    }

    @Override
    public UserPermissionResponse getUserPermissionOfResponse(String userId) throws RuntimeException {
        UserPermission userPermission = this.getUserPermission(userId);
        UserPermissionResponse response = UserPermissionResponse.parse(userPermission);
        return response;
    }

    @Override
    public boolean isHaveViewPermission(String selfUserId, String targetUserId) {
        UserPermission selfUserPermission = this.getUserPermission(selfUserId);
        boolean isNoWantSeeTarget_Self = selfUserPermission.getNoWantToSeeUserList().contains(targetUserId);
        boolean isInBlocked_Self = selfUserPermission.getBlackList().contains(targetUserId);

        UserPermission targetUserPermission = this.getUserPermission(targetUserId);
        boolean isInBlocked_Target = targetUserPermission.getBlackList().contains(selfUserId);
        boolean isInNoAuthority_Target = targetUserPermission.getNoAuthorityUserList().contains(selfUserId);
        // 有一个为否则不允许查看
        return !isNoWantSeeTarget_Self && !isInBlocked_Self && !isInBlocked_Target && !isInNoAuthority_Target;
    }

    public List<String> getHaveViewPermissionUserIds(String selfUserId, List<String> selfFriendUserIds) {
        UserPermission selfUserPermission = this.getUserPermission(selfUserId);
        List<String> haveViewPermissionUserIds = new ArrayList<>(selfFriendUserIds);

        // 筛选不愿意看的好友(黑名单, 我不想看)
        for (String selfFriendUserId : selfFriendUserIds) {
            boolean isNoWantSeeTarget_Self = selfUserPermission.getNoWantToSeeUserList().contains(selfFriendUserId);
            boolean isInBlocked_Self = selfUserPermission.getBlackList().contains(selfFriendUserId);
            if(isNoWantSeeTarget_Self || isInBlocked_Self) {
                haveViewPermissionUserIds.remove(selfFriendUserId);
            }
        }

        // 筛选被好友设置不允许被查看的好友( 黑名单, 不想让他看)
        List<UserPermission> friendPermissions = this.userPermissionActiveRecord.findByUserIds(haveViewPermissionUserIds);
        for (UserPermission friendPermission : friendPermissions) {
            boolean isInBlocked_Target = friendPermission.getBlackList().contains(selfUserId);
            boolean isInNoAuthority_Target = friendPermission.getNoAuthorityUserList().contains(selfUserId);
            if(isInBlocked_Target || isInNoAuthority_Target) {
                haveViewPermissionUserIds.remove(friendPermission.getUserId());
            }
        }

        return haveViewPermissionUserIds;
    }



    public boolean calculateViewPermission(String myUserId, String targetUserId) {
        // 不等于自身的情况下进行判断
        if (!myUserId.equals(targetUserId)) {
            List<String> friendUserIds = this.friendRelationComponentRpcApi.findRealFriendUserIdsByUserId(myUserId);
            boolean isFriend = friendUserIds.contains(targetUserId);
            if (!isFriend) {
                return false;
            }
            boolean isHaveViewPermission = this.isHaveViewPermission(myUserId, targetUserId);
            if (!isHaveViewPermission) {
                return false;
            }
        }
        return true;
    }

    /**
     * 计算是否具有查看权限
     *
     * @param myUserId 自己的ID
     * @return 拥有查看权限的USERID
     */
    public List<String> calculateViewPermission(String myUserId) {

        List<String> friendUserIds = this.friendRelationComponentRpcApi.findRealFriendUserIdsByUserId(myUserId);
        List<String> haveViewPermissionUserIds = this.getHaveViewPermissionUserIds(myUserId, friendUserIds);
        return haveViewPermissionUserIds;
    }





}
